﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BinaryTree
{
    public partial class Form1 : Form
    {
        private DrawTree _drawtree = new DrawTree();
        private BTreeWithRef _bt = new BTreeWithRef();

        public Form1()
        {
            InitializeComponent();
            _drawtree = new DrawTree();
            splitContainer1.Panel2.Controls.Add(_drawtree);
            _drawtree.Dock = DockStyle.Fill;
        }
        private void InitializeGB(bool add, bool del, bool search,bool view)
        {
            GB_Add.Visible = add;
            GB_DelNode.Visible = del;
            GB_Search.Visible = search;
            GB_View.Visible = view;
            
        }

        private void createNewTreeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _bt = new BTreeWithRef();
            _drawtree.Draw(_bt.root);
            InitializeGB(false, false, false,false );
        }

        private void addNewElementToolStripMenuItem_Click(object sender, EventArgs e)
        {
            InitializeGB(true, false, false,false );

        }

        private void Btn_Search_Click(object sender, EventArgs e)
        {
            treeNode tmp=_bt.Search(Convert.ToInt16(MTB_Search.Text));
            _drawtree.Draw(tmp);
        }

        private void Btn_DelNode_Click(object sender, EventArgs e)
        {
            _bt.DelNode(Convert.ToInt16(MTB_DelNode.Text));
            _drawtree.Draw(_bt.root);
        }

        private void Btn_AddNode_Click(object sender, EventArgs e)
        {
            _bt.AddNode(Convert.ToInt16(MTB_AddNode.Text));
            _drawtree.Draw(_bt.root);
        }

        private void nodeSearchToolStripMenuItem_Click(object sender, EventArgs e)
        {
            InitializeGB(false, false, true,false);
        }

        private void elementRemoveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            InitializeGB(false , true , false,false);
        }

        private void heightToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Height of BTree = " + _bt.Height().ToString() + ".", "HEIGHT");
        }

        private void widthToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Width of BTree = " + _bt.Width().ToString() + ".", "WIDHT");
        }

        private void amountOfLeavesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Amount of leaves = " + _bt.AmountOfLeaves().ToString() + ".", "LEAVES");
        }

        private void amountOfNodesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Amount of nodes = " + _bt.AmountOfNodes().ToString() + ".", "NODES");
        }

        private void totalAmountOfElementsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Total amount of nodes = " + _bt.TotalAmountOfNodes().ToString() + ".", "TOTAl AMOUNT");
        }

        private void reversToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _bt.Revers();
            _drawtree.Draw(_bt.root);
        }

        private void viewSortElementsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            InitializeGB(false, false, false, true);
            TB_View.Items.Clear();
            List<int> values = _bt.SortValuePrint();
            for (int i=0;i<values.Count;i++)
            {
                
                TB_View.Items.Add(values[i].ToString());
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
